/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.designer.impl;

import org.soupframework.gwt.designerpanel.client.command.Command;
import org.soupframework.gwt.designerpanel.client.designer.ContainerDesigner;
import org.soupframework.gwt.designerpanel.client.designer.DesignerManager;
import org.soupframework.gwt.designerpanel.client.dragproxy.DragProxy;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class CaptionPanelViewDesigner<T> extends ContainerDesignerBase<T> {

	// variable used to store the content widget when the drag proxy is shown in place of it
	private Widget contentWidget;

	public CaptionPanelViewDesigner(DesignerManager<T> manager, ContainerDesigner<T> parent, T object) {
		super(manager, parent, object);
	}

	public class Operations extends ContainerDesignerBase<T>.Operations {
		@Override
		public void addObject(T object, int left, int top){
			if(getChildObject() != null){
				this.removeObject(getChildObject());
			}
			getObjectView().setContentWidget(getChildObjectView(object));
			super.addObject(object, left, top);
		}

		@Override
		public void removeObject(T object){
			getObjectView().setContentWidget(null);
			super.removeObject(object);
		}
	}

	@Override
	public Operations getOperations(){
		if(this.operations != null){
			return (Operations)this.operations;
		}
		this.operations = this.new Operations();
		return (Operations)this.operations;
	}

	public T getChildObject(){
		if(getChildrenObjects().size() == 1){
			return getChildrenObjects().get(0);
		}
		return null;
	}

	/**
	 * Set the given object as the panel content
	 */
	public void setContent(T object){
		Command command = this.getManager().getCommandFactory().createSetContentCommand(this, object);
		this.getManager().doCommand(command);
	}

	@Override
	public boolean isDropAllowed(T object, int left, int top) {
		return true;
	}

	@Override
	public boolean acceptDrop(T object, int left, int top) {
		if(this.getChildObject() != null){
			String oldObject = this.getObjectDescrition(this.getChildObject());
			String newObject = this.getObjectDescrition(object);
			if(!Window.confirm("Replace object " + oldObject + " with " + newObject + " ?")){
				return false;
			}
		}
		return true;
	}

	@Override
	protected void showDragProxyInt(DragProxy<T> dragProxy, int left, int top){
		if(this.contentWidget == null){
			this.contentWidget = this.getObjectView().getContentWidget();
		}
		this.getObjectView().setContentWidget(dragProxy.getProxyWidget());
	}

	@Override
	protected void hideDragProxyInt(DragProxy<T> dragProxy){
		this.getObjectView().setContentWidget(this.contentWidget);
		this.contentWidget = null;
	}

	@Override
	public abstract CaptionPanel getObjectView();
}
